/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jtmcraft.game.units.states;

import java.util.Random;
import jtmcraft.game.units.GameUnit;
import jtmcraft.graphics.Sprite;

/**
 *
 * @author milosz
 */
public class DefendState implements GameUnitState
{
    int sleepCount;
    GameUnit attacker;
    double angle;
    double direction;
    Random rm;

    public DefendState(GameUnit attacker, double angle, double direction) {
        this.attacker = attacker;
        this.angle = angle;
        this.direction = (-1)*direction;
        this.rm = new Random();
    }

    public void update(GameUnit gameUnit, long timestamp)
    {
        if( !attacker.isLive() ) {
            gameUnit.setState( new MoveState( attacker.getMidX(), attacker.getMidY() ) );
        }
        
        if( null != this.attacker.getState() && this.attacker.getState().getClass() == AttackState.class ) {
            gameUnit.getSprite().setMove(true);
            setDefendAction( gameUnit.getSprite(), this.angle, this.direction );
            if( sleepCount > 0 ) {
                sleepCount--;
            } else {
                sleepCount = 50;
                gameUnit.getActionHandler().hit( gameUnit, attacker, rm.nextInt(Math.abs(gameUnit.getMaxDescrution())) );
            }
        } else {
            gameUnit.setState( null );
            gameUnit.getSprite().setMove(false);
        }
    }

    private void setDefendAction(Sprite sprite, double degree, double i) {
        int dir = (int) (degree / (Math.PI / 8));
        if (i >= 0) {
            switch (dir) {
                case 0:
                    sprite.setSpriteAction(Sprite.eSpriteAction.attackE);
                    break;
                case -1:
                case -2:
                    sprite.setSpriteAction(Sprite.eSpriteAction.attackSE);
                    break;
                case -3:
                    sprite.setSpriteAction(Sprite.eSpriteAction.attackS);
                    break;
                case 1:
                case 2:
                    sprite.setSpriteAction(Sprite.eSpriteAction.attackNE);
                    break;
                case 3:
                    sprite.setSpriteAction(Sprite.eSpriteAction.attackN);
                    break;
            }
        }
        if (i < 0) {
            switch (dir) {
                case 0:
                    sprite.setSpriteAction(Sprite.eSpriteAction.attackW);
                    break;
                case -1:
                case -2:
                    sprite.setSpriteAction(Sprite.eSpriteAction.attackSW);
                    break;
                case -3:
                    sprite.setSpriteAction(Sprite.eSpriteAction.attackN);
                    break;
                case 1:
                case 2:
                    sprite.setSpriteAction(Sprite.eSpriteAction.attackWN);
                    break;
                case 3:
                    sprite.setSpriteAction(Sprite.eSpriteAction.attackS);
                    break;
            }
        }
    }
}